package com.ccl.controller.back;

import com.ccl.entity.Article;
import com.ccl.entity.Comment;
import com.ccl.entity.ResponseData;
import com.ccl.service.ArticleService;
import com.ccl.service.CommentService;
import com.ccl.utils.Commons;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    //跳转到后台首页
    @RequestMapping(value = {"","/index"})
    public String admin(HttpServletRequest request){
        //数据准备
        //1 文章总数
        Integer articlesNum = articleService.getArticlesNum();
        //2 评论总数
        Integer commentsNum = commentService.getCommentsNum();
        //3 最近文章内容
        List<Article> recentArticles = articleService.getRecentArticles(5);
        //4 最近评论内容
        List<Comment> recentComments = commentService.getRecentComments(5);

        //保存数据到request域
        request.setAttribute("articlesNum",articlesNum);
        request.setAttribute("commentsNum",commentsNum);
        request.setAttribute("articles",recentArticles);
        request.setAttribute("comments",recentComments);
        //工具类
        //request.setAttribute("commons",new Commons());
        return "back/index";//跳转到后台首页
    }

    //跳转到发布文章页面
    @RequestMapping("/article/toEditPage")
    public String toEditPage(){

        return "back/article_edit";
    }
    //发布文章
    @RequestMapping("/article/publish")
    @ResponseBody
    public ResponseData publish(Article article){
        articleService.publishArticle(article);
        return ResponseData.OK();
    }
    //跳转到文章管理页面
    @RequestMapping("/article")
    public String article(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1")Integer page, @RequestParam(value = "count",defaultValue = "10") Integer count){
        //判断
        //page=page==null?1:page;
        //分页查询
        PageInfo<Article> articles = articleService.findArticleWithPage(page, count);
        //保存
        request.setAttribute("articles",articles);
        return "back/article_list";
    }
    //跳转到编辑页面
    @RequestMapping("/article/{articleId}")
    public String toEditpage(HttpServletRequest request,@PathVariable("articleId") Integer articleId){
        Article article = articleService.findById(articleId);
        request.setAttribute("contents",article);
        return "back/article_edit";
    }
    //修改文章
    @RequestMapping("/article/modify")
    @ResponseBody
    public ResponseData modify(Article article){
        //修改文章数据
        articleService.updateArticle(article);
        //删除redis中缓存的数据
        return ResponseData.OK();
    }
    //删除文章
    @RequestMapping("/article/delete")
    @ResponseBody
    public ResponseData delete(Integer id){
        //调用删除方法
        articleService.deleteArticle(id);
        return ResponseData.OK();
    }

    //跳转到评论管理页面
    @RequestMapping("/comments")
    public String comments(HttpServletRequest request,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "count",defaultValue = "10")Integer count) {
        //准备数据
        PageInfo<Comment> pageInfo = commentService.findWithPage(page, count);
        request.setAttribute("comments",pageInfo);
        return "back/comment_list";
    }
    //删除评论
    @RequestMapping("/comment/delete")
    @ResponseBody
    public ResponseData commentDelete(Integer id){
        commentService.deleteById(id);
        return ResponseData.OK();
    }
}
